/*
 *  四川生学教育科技有限公司
 *  Copyright (c) 2015-2025 Founder Ltd. All Rights Reserved.
 *
 *  This software is the confidential and proprietary information of
 *  Founder. You shall not disclose such Confidential Information
 *  and shall use it only in accordance with the terms of the agreements
 *  you entered into with Founder.
 */

package com.greateffect.littlebud.lib.utils;

import android.content.Context;
import android.text.TextUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by daiyong on 2017/4/5.
 * E-mail:dyong@sxw.cn
 * add:成都天府软件园E3-3F
 */

public class StringUtils {

    public final static String[] FORMAT_TXT = {".txt",".TXT"};
    public final static String[] FORMAT_WORD = {".doc", ".docx"};
    public final static String[] FORMAT_PPT = {".ppt", ".pptx"};
    public final static String[] FORMAT_EXCEL = {".xls", ".xlsx"};
    public final static String[] FORMAT_PDF = {".pdf"};
    public final static String[] FORMAT_IMAGE = {".jpg", ".jpeg", ".png", ".bmp", ".JPG", ".JPEG", ".PNG", ".BMP",".gif",".GIF",".webp"};
    public final static String[] FORMAT_ZIP = {".zip",".ZIP"};
    public final static String[] FORMAT_VIDEO = {".mp4", ".MP4", ".3gp", ".3GP"};
    public final static String[] FORMAT_AUDIO = {".mp3", ".MP3", ".amr", ".AMR"};
    public final static String[] FORMAT_MP3 = {".mp3", ".MP3"};


    /**
     * url是否是txt文件
     * @param url
     * @return
     */
    public static boolean isTxt(String url) {
        boolean isTxt = false;
        for (String type : FORMAT_TXT) {
            if (url.endsWith(type)) {
                isTxt = true;
                break;
            }
        }
        return isTxt;
    }

    /**
     * url是否是zip文件
     * @param url
     * @return
     */
    public static boolean isZip(String url){
        boolean isZip = false;
        for (String type : FORMAT_ZIP) {
            if (url.endsWith(type)) {
                isZip = true;
                break;
            }
        }
        return isZip;
    }

    /**
     * 判断字符串是否是数字
     * @param str
     * @return
     */
    public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }

    /**
     * 获取名字最后一个字
     */
    public static String getLastName(String name) {
        if (TextUtils.isEmpty(name)) {
            return "无";
        }
        int len = name.length();
        return name.substring(len - 1, len);
    }

    /**
     * 判断url是否是图片
     * @param url
     * @return
     */
    public static boolean isPicUrl(String url) {
        if(TextUtils.isEmpty(url)){
            return false;
        }
        boolean isPic = false;
        for (String type : FORMAT_IMAGE) {
            if (url.endsWith(type)) {
                isPic = true;
                break;
            }
        }
        return isPic;
    }

    /**
     * 判断url是否是视频
     * @param url
     * @return
     */
    public static boolean isVideoUrl(String url) {
        boolean isVideo = false;
        if(!TextUtils.isEmpty(url)){
            for (String type : FORMAT_VIDEO) {
                if (url.endsWith(type)) {
                    isVideo = true;
                    break;
                }
            }
        }
        return isVideo;
    }

    /**
     * 判断是否是mp4
     * @param url
     * @return
     */
    public static boolean isMp4(String url){
        String suffix = url.substring(url.lastIndexOf("."));
        return ".mp4".equalsIgnoreCase(suffix);
    }

    /**
     * 判断url是否是MP3文件
     * @param url
     * @return
     */
    public static boolean isMP3(String url) {
        boolean isMP3 = false;
        if(!TextUtils.isEmpty(url)){
            for (String type : FORMAT_MP3) {
                if (url.endsWith(type)) {
                    isMP3 = true;
                    break;
                }
            }
        }
        return isMP3;
    }

    /**
     * 判断url是否是音频
     * @param url
     * @return
     */
    public static boolean isAudio(String url) {
        boolean isVideo = false;
        if(!TextUtils.isEmpty(url)){
            for (String type : FORMAT_AUDIO) {
                if (url.endsWith(type)) {
                    isVideo = true;
                    break;
                }
            }
        }
        return isVideo;
    }

    /**
     * 是否是pdf
     * @param url
     * @return
     */
    public static boolean isPDF(String url){
        boolean isVideo = false;
        if(!TextUtils.isEmpty(url)){
            url = url.toLowerCase();
            for (String type : FORMAT_PDF) {
                if (url.endsWith(type)) {
                    isVideo = true;
                    break;
                }
            }
        }
        return isVideo;
    }

    /**
     * 是否是pdf
     * @param url
     * @return
     */
    public static boolean isWord(String url){
        boolean isWord = false;
        if(!TextUtils.isEmpty(url)){
            url = url.toLowerCase();
            for (String type : FORMAT_WORD) {
                if (url.endsWith(type)) {
                    isWord = true;
                    break;
                }
            }
        }
        return isWord;
    }

    /**
     * 是否是pdf
     * @param url
     * @return
     */
    public static boolean isPPT(String url){
        boolean isppt = false;
        if(!TextUtils.isEmpty(url)){
            url = url.toLowerCase();
            for (String type : FORMAT_PPT) {
                if (url.endsWith(type)) {
                    isppt = true;
                    break;
                }
            }
        }
        return isppt;
    }

    /**
     * 是否是pdf
     * @param url
     * @return
     */
    public static boolean isExcel(String url){
        boolean isExcel = false;
        if(!TextUtils.isEmpty(url)){
            url = url.toLowerCase();
            for (String type : FORMAT_EXCEL) {
                if (url.endsWith(type)) {
                    isExcel = true;
                    break;
                }
            }
        }
        return isExcel;
    }

    /**
     * 文档类：doc、docx、txt

     演示类：ppt、pptx

     表格类：xls、xlsx
     * @param url
     * @return
     */
    public static boolean isPWFile(String url){
        boolean isVideo = false;
        if(!TextUtils.isEmpty(url)){
            String[] audioTypes = {".doc", ".docx", ".ppt", ".pptx", ".xls", ".xlsx", ".pdf"};
            for (String type : audioTypes) {
                if (url.endsWith(type)||url.endsWith(type.toUpperCase())) {
                    isVideo = true;
                    break;
                }
            }
        }
        return isVideo;
    }

    /**
     * 获取文件后缀
     * @param filePath
     * @return
     */
    public static String getSuffix(String filePath){
        if(TextUtils.isEmpty(filePath)){
            return "";
        }
        try {
            return filePath.substring(filePath.lastIndexOf("."),filePath.length());
        }catch (Exception e){
        }
        return "";
    }

    public static String getFileName(String filePath){
        try {
            return filePath.substring(filePath.lastIndexOf("/")+1,filePath.lastIndexOf("."));
        }catch (Exception e){
        }
        return "";
    }

    public static String getFileNameAndSuffix(String filePath){
        try {
            return filePath.substring(filePath.lastIndexOf("/")+1,filePath.length());
        }catch (Exception e){
        }
        return "";
    }

    /**
     * 验证手机号
     * @param phoneNo
     * @return
     */
    public static boolean isMobileNO(String phoneNo){
        if(phoneNo.length() == 11){
            return true;
        }
        return false;
    }

    /**
     * 获取Stirng资源文件里面的字符串
     * @param context
     * @param resourceId
     * @return
     */
    public static String getStringFromValueXml(Context context, int resourceId){
        try {
            return context.getResources().getString(resourceId);
        }catch (Exception e){
        }
        return "";
    }

    /**
     * 获取Stirngarray资源文件里面的字符串
     * @param context
     * @param resourceId
     * @return
     */
    public static String[] getStringArrayFromValueXml(Context context, int resourceId){
        try {
            return context.getResources().getStringArray(resourceId);
        }catch (Exception e){
        }
        return null;
    }

    /**
     * 得到pcdn url
     * @param url
     * @return
     */
    public static String getPcdnVideoUrl(String url){
//        if(url.startsWith("http://")){
//            return PcdnManager.PCDNAddress(PcdnType.DOWN,url);
//        }
        return url;
    }

    /**
     * 得到半分比
     * @param percent
     * @return
     */
    public static String getPercentStr(float percent){
        return ((int)(percent*100))+"%";
    }

    /**
     * 得到半分比
     * @param percent
     * @return
     */
    public static String getPercentOneStr(float percent){
        try {
            BigDecimal bd = new BigDecimal(percent * 100);
            bd = bd.setScale(1, BigDecimal.ROUND_HALF_UP);
            return bd + "%";
        } catch (Exception e) {
            return "0%";
        }
    }


    /**
     * 检测账号是否正常
     *
     * @param account
     * @return
     */
    public static int checkAccount(String account) {
        if (TextUtils.isEmpty(account)) {
            return -1;
        }
        if (account.length() == 11) {
            return 0;
        }
        if (account.length() == 18 || account.length() == 15) {
            return 1;
        }
        return -1;
    }

    public static boolean isContainChinese(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }

    public static String getChineseToLetter(String str){
        if(isContainChinese(str)){
            str = System.currentTimeMillis()+str.substring(str.lastIndexOf("."),str.length());
        }
        return str;
    }

    public static int getScoreInt(float score){
        if((score*10)%10 == 0){
            return (int) score;
        }else{
            return (int) score+1;
        }
    }

    public static int getScoreInt(String scoreStr){
        try {
            float score = Float.parseFloat(scoreStr);
            if((score*10)%10 == 0){
                return (int) score;
            }else{
                return (int) score+1;
            }
        }catch (Exception e){
        }
        return 0;
    }

    public static String getScoreShow(float score){
        if((score*10)%10 == 0){
            return ((int) score)+"";
        }else{
            return ""+score;
        }
    }

    public static String getNumOneFloat(float score){
        try {
            if((score*10)%10 == 0)
                return ((int) score)+"";
            DecimalFormat df = new DecimalFormat("#0.0");
            return df.format(score);
        }catch (Exception e){

        }
        return "0";
    }

}
